import { loadResourceList } from '@/utils/common';
import { MAP_DEFAULT_CENTER } from '@/config';

export const mapThemeStyleIdMap = {
  light: import.meta.env.VITE_MAP_STYLE_ID_LIGTH,
  dark: import.meta.env.VITE_MAP_STYLE_ID_DARK,
  satellite: import.meta.env.VITE_MAP_STYLE_ID_SATELLITE
}

export const loadMapSource = (cb) => {
  const { VITE_MAP_URL_CSS, VITE_MAP_JS, VITE_MAP_JS_PLUGINS } = import.meta.env;
  const sourceList = [
    { url: VITE_MAP_URL_CSS, type: 'css' },
    { url: VITE_MAP_JS, type: 'js' },
    { url: VITE_MAP_JS_PLUGINS, type: 'js' }
  ];
  return loadResourceList(sourceList, cb).then(() => {
    minemap.domainUrl = import.meta.env.VITE_MAP_URL_DOMAIN;
    minemap.dataDomainUrl = import.meta.env.VITE_MAP_URL_DATADOMAIN;
    minemap.serverDomainUrl = import.meta.env.VITE_MAP_URL_SERVERDOMAIN;
    minemap.spriteUrl = import.meta.env.VITE_MAP_URL_SPRITE;
    minemap.serviceUrl = import.meta.env.VITE_MAP_URL_SERVICE;
    console.log('----- map load success -----');
  })
}

export const loadMapEditSource = (cb) => {
  const { VITE_MAP_URL_CSS_EDIT, VITE_MAP_JS_EDIT } = import.meta.env;
  const sourceList = [{ url: VITE_MAP_URL_CSS_EDIT, type: 'css' },{ url: VITE_MAP_JS_EDIT, type: 'js' }];
  return loadResourceList(sourceList, cb)
}

export const getMapInstance = (options = {}) => {
  options = {
    theme: 'light',
    pitch: 0, // 俯仰角度
    zoom: 10, // 默认缩放等级 zoom值越小 视野越大
    minZoom: 10, // 最小缩放等级，缩小
    maxZoom: 18, // 最大缩放等级，放大
    center: MAP_DEFAULT_CENTER,
    ...options
  }
  return new minemap.Map({
    container: options.domRef || options.elId,
    style: `${import.meta.env.VITE_MAP_URL_STYLE}${mapThemeStyleIdMap[options.theme]}`, /*底图样式*/
    center: options.center,
    pitch: options.pitch,
    zoom: options.zoom,
    minZoom: options.minZoom,
    maxZoom: options.maxZoom,
    projection: import.meta.env.VITE_MAP_PROJECTION,
    logoControl: false
  });
}